﻿using System;
using System.Collections.Generic;
using System.Text;
using centrafuse.Plugins;
using System.Windows.Forms;
using System.IO;

namespace Grooveshark
{
    public class Setup : CFSetup
    {
        //shamelessly copied from Justin-Credible's Pandora plugin
        internal const string PASSWORD = "336C178F-454A-46A5-8C7F-3CE822C08FCF";

        // The setup constructor will be called each time this plugin's setup is opened from the CF Setting Page
        // This setup is opened as a dialog from the CF_pluginShowSetup() call into the main plugin application form.
        public Setup(ICFMain mForm, ConfigReader config, LanguageReader lang)
        {
            // MainForm must be set before calling any Centrafuse API functions
            this.MainForm = mForm;

            // pluginConfig and pluginLang should be set before calling CF_initSetup() so this CFSetup instance 
            // will internally save any changed settings.
            this.pluginConfig = config;
            this.pluginLang = lang;

            // When CF_initSetup() is called, the CFPlugin layer will call back into CF_setupReadSettings() to read the page
            // Note that this.pluginConfig and this.pluginLang must be set before making this call
            CF_initSetup(1, 1);

            // Update the Settings page title
            this.CF_updateText("TITLE", this.pluginLang.ReadField("/APPLANG/SETUP/TITLE"));
        }

        public override void CF_setupReadSettings(int currentpage, bool advanced)
        {
            try
            {
                ButtonHandler[CFSetupButton.One] = new CFSetupHandler(SetUserName);
                ButtonText[CFSetupButton.One] = this.pluginLang.ReadField("/APPLANG/SETUP/USERNAME");
                ButtonValue[CFSetupButton.One] = this.pluginConfig.ReadField("/APPCONFIG/USERNAME");

                ButtonHandler[CFSetupButton.Two] = new CFSetupHandler(SetPassword);
                ButtonText[CFSetupButton.Two] = this.pluginLang.ReadField("/APPLANG/SETUP/PASSWORD");
                string encryptedPassword = this.pluginConfig.ReadField("/APPCONFIG/PASSWORD");
                ButtonValue[CFSetupButton.Two] = String.IsNullOrEmpty(encryptedPassword) ? String.Empty : new String('•', 8);

                //ButtonHandler[CFSetupButton.Three] = new CFSetupHandler(SetBufferSize);
                //ButtonText[CFSetupButton.Three] = this.pluginLang.ReadField("/APPLANG/SETUP/BUFFERSIZE");
                //ButtonValue[CFSetupButton.Three] = GetBufferSizeString();

                ButtonHandler[CFSetupButton.Five] = new CFSetupHandler(SetAutoLogin);
                ButtonText[CFSetupButton.Five] = this.pluginLang.ReadField("/APPLANG/SETUP/LOGIN");
                ButtonValue[CFSetupButton.Five] = this.pluginConfig.ReadField("/APPCONFIG/LOGIN");

                ButtonHandler[CFSetupButton.Six] = new CFSetupHandler(SetLogEvents);
                ButtonText[CFSetupButton.Six] = this.pluginLang.ReadField("/APPLANG/SETUP/LOGEVENTS");
                ButtonValue[CFSetupButton.Six] = this.pluginConfig.ReadField("/APPCONFIG/LOGEVENTS");
            }
            catch (Exception errmsg) { CFTools.writeError(errmsg.Message, errmsg.StackTrace); }
        }

        private void SetUserName(ref object value)
        {
            try
            {
                object tempobject;
                string resultvalue, resulttext;

                // Display OSK for user to type display name
                DialogResult dialogResult = this.CF_systemDisplayDialog(CF_Dialogs.OSK, this.pluginLang.ReadField("/APPLANG/SETUP/USERNAME"), ButtonValue[(int)value], null, out resultvalue, out resulttext, out tempobject, null, true, true, true, true, false, false, 1);

                if (dialogResult == DialogResult.OK)
                {
                    // save user value, note this does not save to file yet, as this should only be done when user confirms settings
                    // being overwritten when they click the "Save" button.  Saving is done internally by the CFSetup instance if
                    // pluginConfig and pluginLang were properly set before callin CF_initSetup().
                    this.pluginConfig.WriteField("/APPCONFIG/USERNAME", resultvalue);

                    // Display new value on Settings Screen button
                    ButtonValue[(int)value] = resultvalue;
                }
            }
            catch (Exception errmsg) { CFTools.writeError(errmsg.Message, errmsg.StackTrace); }
        }

        private void SetPassword(ref object value)
        {
            try
            {
                object tempobject;
                string resultvalue, resulttext;

                // Display OSK for user to type password
                DialogResult dialogResult = this.CF_systemDisplayDialog(CF_Dialogs.OSK, this.pluginLang.ReadField("/APPLANG/SETUP/PASSWORD"), String.Empty, "PASSWORD", out resultvalue, out resulttext, out tempobject, null, true, true, true, true, false, false, 1);

                if (dialogResult == DialogResult.OK)
                {
                    // save user value, note this does not save to file yet, as this should only be done when user confirms settings
                    // being overwritten when they click the "Save" button.  Saving is done internally by the CFSetup instance if
                    // pluginConfig and pluginLang were properly set before callin CF_initSetup().
                    this.pluginConfig.WriteField("/APPCONFIG/PASSWORD", EncryptionHelper.EncryptString(resultvalue, PASSWORD));

                    // Display new value on Settings Screen button
                    ButtonValue[(int)value] = new String('•', 8);
                }
            }
            catch (Exception errmsg) { CFTools.writeError(errmsg.Message, errmsg.StackTrace); }
        }

        private string GetBufferSizeString()
        {
            string currentBufferSizeStr = this.pluginConfig.ReadField("/APPCONFIG/BUFFERSIZE");
            int currentBufferSize = 0;
            if (!int.TryParse(currentBufferSizeStr, out currentBufferSize))
                currentBufferSize = 250;

            return currentBufferSize.ToString();
        }

        private void SetBufferSize(ref object value)
        {
            try
            {
                string currentBufferSizeStr = GetBufferSizeString();

                CFControls.CFListViewItem[] audioFormatItems = new CFControls.CFListViewItem[5];
                audioFormatItems[0] = new CFControls.CFListViewItem("100", "100", false);
                audioFormatItems[1] = new CFControls.CFListViewItem("250", "250", false);
                audioFormatItems[2] = new CFControls.CFListViewItem("500", "500", false);
                audioFormatItems[3] = new CFControls.CFListViewItem("1000", "1000", false);
                audioFormatItems[4] = new CFControls.CFListViewItem("99999", "99999", false);

                object resultObject;
                string resultvalue, resulttext;
                
                DialogResult result = this.CF_systemDisplayDialog(CF_Dialogs.FileBrowser, this.pluginLang.ReadField("/APPLANG/SETUP/AUDIOFORMAT"), null, currentBufferSizeStr, out resultvalue, out resulttext, out resultObject, audioFormatItems, false, true, false, false, false, false, 1);

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    this.pluginConfig.WriteField("/APPCONFIG/BUFFERSIZE", resultvalue);
                    ButtonValue[(int)value] = resultvalue;
                }
                
            }
            catch (Exception errmsg) { CFTools.writeError(errmsg.Message, errmsg.StackTrace); }
        }

        private void SetLogEvents(ref object value)
        {
            // save user value, note this does not save to file yet, as this should only be done when user confirms settings
            // being overwritten when they click the "Save" button.  Saving is done internally by the CFSetup instance if
            // pluginConfig and pluginLang were properly set before callin CF_initSetup().
            this.pluginConfig.WriteField("/APPCONFIG/LOGEVENTS", value.ToString());
        }

        private void SetAutoLogin(ref object value)
        {
            this.pluginConfig.WriteField("/APPCONFIG/LOGIN", value.ToString());
        }
    }
}
